using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.ComponentModel;

namespace GraphAlgsPresentation.ScriptLibrary
{
    /// <summary>
    /// Provides a set text command
    /// </summary>
    [Serializable]
    public class SetText : Command
    {
        private int _index;

        /// <summary>
        /// Gets or sets the index.
        /// </summary>
        /// <value>The index.</value>
        public int Index
        {
            get { return _index; }
            set { _index = value; }
        }

        private string _text;

        /// <summary>
        /// Gets or sets the text.
        /// </summary>
        /// <value>The text.</value>
        public string Text
        {
            get { return _text; }
            set { _text = value; }
        }

        private string oldText;

        /// <summary>
        /// Gets or sets the old text.
        /// </summary>
        /// <value>The old text.</value>
        [XmlIgnore]
        [Browsable(false)]
        public string OldText
        {
            get { return oldText; }
            set { oldText = value; }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SetText"/> class.
        /// </summary>
        public SetText()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SetText"/> class.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <param name="text">The text.</param>
        public SetText(int index, string text)
        {
            this.Index = index;
            this.Text = text;
        }

        /// <summary>
        /// Runs this instance.
        /// </summary>
        public override void Run()
        {
            if (Index>=this.Context.CommentControl.Lines.Count)
                oldText = "";
            else oldText = this.Context.CommentControl.Lines[Index].Text;
            this.Context.CommentControl.SetText(Index, Text);
        }

        /// <summary>
        /// Undoes this instance.
        /// </summary>
        public override void Undo()
        {
            this.Context.CommentControl.SetText(Index, oldText);
        }
    }
}
